<?php

/**
 * Description of jornada
 *
 * @author Leonardo Lino
 */
class Jornada extends CI_Controller {

    private $smarty;

    public function __construct() {
        parent::__construct();
        $session_id = $this->session->userdata('session_id');
        require_once 'smartyLoader.php';
        $this->smarty = $smarty;
        $this->load->model('jornadaModel');
        $this->load->model('epocaModel');
        $this->load->model('problemaModel');
        $this->user->isAccessBackOffice($this->smarty);
    }

    public function index() {

        $jornadas = $this->jornadaModel->loadAll();
        if ($jornadas == null) {
            $this->smarty->assign("vazio", 0);
        } else {
            $this->smarty->assign("vazio", 1);
        }

        
        $problemas = $this->problemaModel->loadAll();
        $epocas = $this->epocaModel->loadAll();
        
        if ($problemas==null || $epocas == null){
          $this->smarty->assign("notok", true);  
        }

        $css = array("admin/style.css");
        $this->smarty->assign("cssFileList", $css);
        $this->smarty->assign("problemas", $this->problemaModel->loadAll());
        $this->smarty->assign("epocas", $this->epocaModel->loadAll());
        $this->smarty->assign("admins", $this->user->getAdmins());
        $this->smarty->assign("jornadas", $jornadas);
        $this->smarty->assign("templateActivo", "jornada.tpl.html");
        $this->smarty->display('admin/header.tpl.html');
        $this->smarty->display('admin/main.tpl.html');
        $this->smarty->display('admin/footer.tpl.html');
    }

    public function delete() {
        $resultado = $this->jornadaModel->deleteFromId($_GET['vid']);
        $this->index();
    }

    public function novo() {

        $state = $_POST['state'];
        if ($state == "1") {
            $resultado = $this->jornadaModel->novo($_POST['nome'], $_POST['id_autor'], $_POST['id_epoca'], $_POST['data_ini'], $_POST['data_fim'], $_POST['id_problema']);
            $this->index();
        } else {
            $resultado = $this->jornadaModel->update($_POST['vid'], $_POST['nome'], $_POST['id_autor'], $_POST['id_epoca'], $_POST['data_ini'], $_POST['data_fim'], $_POST['id_problema']);
            $this->index();
        }
    }

    public function loadAjax() {
        $row = $this->jornadaModel->loadFromId($_POST['vid']);
        echo json_encode($row);
    }

}

?>
